package com.wztk.shydemo;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.test.InstrumentationTestRunner;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.fi.common.http.FiHttpRequest;
import com.wztk.shydemo.proxy.*;
import com.wztk.shydemo.yumi.InteractiveYumi;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;


public class MainActivity extends AppCompatActivity {
    Button btnOK;
    Button btnCancel;
    Button btnTmpTest;
    Button btnTmpTest2;

    Button btnProxyStart;
    Button btnProxyStop;
    Button btnGetRoot;
    Button btnLockTest;
    Button btnAndroidIdTest;
    EditText edtTxtShow;

    private InteractiveYumi interactiveYumi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android.app.Instrumentation test;
        //GsonDemo测试用例
//        GsonDemoMain gdm = new GsonDemoMain();
//        String ret = gdm.start();
//        EditText edtTxtShow = (EditText) findViewById(R.id.edtTxtShow);
//        edtTxtShow.setText(ret);


        btnTmpTest  = (Button)findViewById(R.id.btnTmpTest);
        btnTmpTest2  = (Button)findViewById(R.id.btnTmpTest2);
        btnOK  = (Button)findViewById(R.id.btnOk);
        btnCancel  = (Button)findViewById(R.id.btnCancel);
        btnLockTest = (Button)findViewById(R.id.btnLocktest);

        btnAndroidIdTest = (Button)findViewById(R.id.btnAndroidIdTest);


        //获取编辑框，并设置自动滚动
        edtTxtShow = (EditText) findViewById(R.id.edtTxtShow);
        edtTxtShow.setMovementMethod(ScrollingMovementMethod.getInstance());
        edtTxtShow.setSelection(edtTxtShow.getText().length(), edtTxtShow.getText().length());
        btnProxyStart = (Button)findViewById(R.id.btnProxyStart);
        btnProxyStop = (Button)findViewById(R.id.btnProxyStop);
        btnGetRoot = (Button)findViewById(R.id.btnGetRoot);


        //interactiveYumi = new InteractiveYumi("mirst1000", "a5265050");


        //===========================================================================
        //获取root按钮
        //===========================================================================
        btnAndroidIdTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
                edtTxtShow.setText(androidId);
            }
        });



        //===========================================================================
        //获取root按钮
        //===========================================================================
        btnGetRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String apkRoot="chmod 777 " + getPackageCodePath();
                RootManager.RootCommand(apkRoot);
            }
        });

        //===========================================================================
        //代理启动按钮
        //===========================================================================
        btnProxyStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //EditText edtTxtShow = (EditText)findViewById(R.id.edtTxtShow);
                ProxyManager.ProxyManagerArgs pra = new ProxyManager.ProxyManagerArgs();
                pra.setServerUrl("http://192.168.0.101:8080/sys/getProxyInfo");
                pra.setDomain("http://vip666.daili666api.com/ip/");
                pra.setTid("556226773217439");
                pra.setNum("1");
                pra.setArea("");
                pra.setUid("10089");
                pra.setPort1("8123");
                pra.setPort2("8124");
                pra.setWorkdir("/data/local/tmp/proxy_test");
                ProxyManager pm = new ProxyManager(pra);
                try {
                    pm.setProxy();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        //===========================================================================
        //锁测试按钮
        //===========================================================================
        btnLockTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                File f = new File("/data/local/tmp/locktest");
                try {
                    if (!f.exists())
                        f.createNewFile();

                    RandomAccessFile in = new RandomAccessFile(f, "rw");
                    FileChannel fcout = in.getChannel();
                    FileLock flout = null;
                    while (true) {
                        try {
                            flout = fcout.tryLock();
                            break;
                        } catch (Exception e) {
                            Thread.sleep(1000);
                        }
                    }

                    //String str = in.toString();
                    int len =  (int)in.length();
                    byte[] buf = new byte[len];
                    in.read(buf);
                    String str = new String(buf);

                    flout.release();
                    fcout.close();
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        //===========================================================================
        //代理停止按钮
        //===========================================================================
        btnProxyStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProxyManager.stopProxy("/data/local/tmp/proxy_test");
            }
        });

        //===========================================================================
        //临时测试按钮
        //===========================================================================
        btnTmpTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPkgUid ();
            }
        });

        //===========================================================================
        //短信测试按钮
        //===========================================================================
        btnTmpTest2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    interactiveYumi = InteractiveYumi.create("mirst1000", "a5265050");
                    if (null != interactiveYumi) {
                        interactiveYumi.getMobilenum("1389", "", "");
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //getPkgUid ();
            }
        });


        //===========================================================================
        //确认按钮
        //===========================================================================
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        //===========================================================================
        //取消按钮
        //===========================================================================
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }



    public String getPkgUid (){
        String ret = "";
        String cmds = "chmod 777 /data/system/packages.list";
        ShellUtils.execCommand(cmds, true);
        cmds = "cat /data/system/packages.list |grep com.jky.earn100";
        ShellUtils.CommandResult cret = ShellUtils.execCommand(cmds, true);
        String[] cretArray =  cret.successMsg.split(" ");
        ret = cretArray[1];
        return ret;
    }
}
